package com.sc.es.controller.backend;

import com.sc.es.common.ApiResponse;
import com.sc.es.common.ServiceResult;
import com.sc.es.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author sc
 * Created on  2018/2/7
 */
@RestController
@RequestMapping("/address/support")
public class HouseAddressController {

    @Autowired
    private IAddressService addressService;
    /**
     * 市
     * @return
     */
    @GetMapping("/cities")
    public ApiResponse getSupportCities(){
       return CommonPart(addressService.findAllCitiesByLevel());
    }
    /***
     * 区县
     * @param cityName
     * @return
     */
    @GetMapping("/regions")
    public ApiResponse getRegions(@RequestParam("city_name")String cityName){
        return CommonPart(addressService.findAllRegionsByCity(cityName));
    }

    /**
     * 地铁线
     * @param cityName
     * @return
     */
    @GetMapping("/subway/line")
    public ApiResponse getSubWayLine(@RequestParam("city_name")String cityName){
        return CommonPart(addressService.findSubWay(cityName));
    }

    /**
     * 地铁站
     * @param subwayId
     * @return
     */
    @GetMapping("/subway/station")
    public ApiResponse getSubWayStation(@RequestParam("subway_id")Integer subwayId){
        return CommonPart(addressService.findSubWayStation(subwayId));
    }

    /***
     * 封装公共的代码部分
     * @param serviceResult
     * @return
     */
    private ApiResponse CommonPart(ServiceResult serviceResult){
        if(serviceResult.getResult().isEmpty()){
            return ApiResponse.ofStatus(ApiResponse.Status.NOT_FOUND);
        }
        return ApiResponse.ofSuccess(serviceResult.getResult() );
    }
}
